package JavaSE.TCPUDPLearning.JudgeConnectingIP;

import java.io.IOException;
import java.net.InetAddress;

/**
 *  测试 查询该局域网(192.168.5.xxx)中的可用IP地址
 */
public class Demo {
    public static void main(String[] args) throws IOException {
        ring();
    }
    public static void ring() throws IOException {

        System.out.println("如下ip地址可以连接");
        //本机ip地址192.
        for(int i=0;i<125;i++) {
            String s="192.168.5."+i;
            isReachable(s);
        }
        System.out.println("总共有："+num+"个地址");
    }
    static int num=0;
    public static void isReachable(String remoteInetAddr) {
        boolean reachable=false;
        try {
            InetAddress address=InetAddress.getByName(remoteInetAddr);
            reachable=address.isReachable(1500);
        }catch(Exception e) {
            e.printStackTrace();
        }
        if(reachable) {
            System.out.println(remoteInetAddr);
            num++;
        }
    }
}
